﻿
Imports osi.root.delegates
Imports osi.root.connector

Namespace queue_runner
    Public Module _queue_runner_check
        Public Function check(ByVal c As _do(Of Boolean),
                              ByVal cb As void,
                              ByRef f As _do(Of Boolean)) As Boolean
            If c Is Nothing OrElse cb Is Nothing Then
                Return False
            Else
                f = Function() As Boolean
                        If do_(c, True) Then
                            cb()
                            Return False
                        Else
                            Return True
                        End If
                    End Function
                Return True
            End If
        End Function

        Public Function check(ByVal c As _do(Of Boolean),
                              ByVal cb As void,
                              ByVal timeout_ms As Int64,
                              ByRef f As _do(Of Boolean)) As Boolean
            If timeout_ms < 0 Then
                Return check(c, cb, f)
            ElseIf c Is Nothing Then
                Return False
            Else
                Dim till As Int64 = 0
                till = nowadays.milliseconds() + timeout_ms
                Return check(Function() As Boolean
                                 Return c() OrElse
                                        (nowadays.milliseconds() >= till)
                             End Function,
                             cb,
                             f)
            End If
        End Function

        Public Function check(ByVal c As _do(Of Boolean),
                              ByVal cb As void) As _do(Of Boolean)
            Dim f As _do(Of Boolean) = Nothing
            assert(check(c, cb, f))
            Return f
        End Function

        Public Function check(ByVal c As _do(Of Boolean),
                              ByVal cb As void,
                              ByVal timeout_ms As Int64) As _do(Of Boolean)
            Dim f As _do(Of Boolean) = Nothing
            assert(check(c, cb, timeout_ms, f))
            Return f
        End Function
    End Module
End Namespace
